
THIS_DIR=$(cd $(dirname $0); pwd)
PROJECT_NAME=$(basename ${THIS_DIR})

[ "$TAG_VER" = "" ] && TAG_VER=0.0.0 
[ "$BUILD_NUMBER" = "" ] && BUILD_NUMBER=1

VERSION=${TAG_VER}.${BUILD_NUMBER}

echo "Project: ${PROJECT_NAME}"
echo "Version: ${VERSION}"


echo Create Revision File ...
[ ! -d include ] && mkdir include
echo "//" >  include/revision.h
echo "#define PROJECT_NAME \"${PROJECT_NAME}\""  >>  include/revision.h
echo "#define BOARD_NAME \"${BOARD}\""  >>  include/revision.h
echo "#define VERSION_STRING \"${VERSION}\""  >>  include/revision.h


echo Compile ...
[ ! -d build ] && mkdir build
[ -d build/output ] && rm -rf build/output
mkdir build/output
cd build
cd output
cmake -G Ninja ../.. || exit 1
ninja  || exit 1
cd ..
cd ..


echo Output Files ...
[ -d output ] && rm -rf output
mkdir output
mkdir output/files
[ -f build/output/${PROJECT_NAME}.exe ] && cp build/output/${PROJECT_NAME}.exe output/files
[ -f build/output/${PROJECT_NAME} ] && cp build/output/${PROJECT_NAME} output/files
[ -d resource ] && cp -r resource/* output/files
[ -f output/files/app.info ] && echo "VERSION=${VERSION}">> output/files/app.info 
if [ ! "${OUTPUT_NAME}" = "" ]; then
	[ -f output/files/${PROJECT_NAME} ] && mv output/files/${PROJECT_NAME} output/files/${OUTPUT_NAME}
	[ -f output/files/${PROJECT_NAME}.exe ] && mv output/files/${PROJECT_NAME}.exe output/files/${OUTPUT_NAME}.exe
fi

echo Archive Files ...
cd output
cd files
OUTPUT_ARCHIVE=${PROJECT_NAME}_${VERSION}
if [ ! "${OUTPUT_NAME}" = "" ]; then
	OUTPUT_ARCHIVE=${OUTPUT_NAME}_${VERSION}
fi
if [ ! "${PLATFORM}" = "" ]; then
	OUTPUT_ARCHIVE=${OUTPUT_ARCHIVE}_${PLATFORM}
fi
zip -r ../${OUTPUT_ARCHIVE}.zip ./* || exit 1
cd ..
cd ..

echo Build OK



